export const DATEIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
export const DATEIME_FORMAT_TWO = 'YYYY-MM-DD';
export const DATEIME_FORMAT_MINUTE = 'YYYY-MM-DD HH:mm';

export function issetParam(param: any): boolean {
  if (param === null || param === undefined) {
    return false;
  }
  return true;
}

export function setLoginInfo(data: {}) {
  localStorage.setItem('user-login-info', JSON.stringify(data));
}

export function isLogin(): boolean {
  const loginInfoStr = localStorage.getItem('user-login-info');
  if (!loginInfoStr) return false;
  const loginInfo = JSON.parse(loginInfoStr);

  if (issetParam(loginInfo) && issetParam(loginInfo.api_token)) {
    return true;
  }
  return false;
}

export function getBdToken(): string {
  const loginInfoStr = localStorage.getItem('user-login-info');
  if (!loginInfoStr) return '';

  const loginInfo = JSON.parse(loginInfoStr);
  if (loginInfo) {
    return loginInfo.api_token;
  } else {
    return '';
  }
}
export function getLang(): string {
  const lang = localStorage.getItem('lang_code');
  if (lang) {
    return lang;
  } else {
    return 'zh-CN';
  }
}

export function IsURL(str_url: string): boolean {
  var strRegex =
    '^((https|http|ftp|rtsp|mms)?://)' +
    "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" + //ftp的user@
    '(([0-9]{1,3}.){3}[0-9]{1,3}' + // IP形式的URL- 199.194.52.184
    '|' + // 允许IP和DOMAIN（域名）
    "([0-9a-z_!~*'()-]+.)*" + // 域名- www.
    '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].' + // 二级域名
    '[a-z]{2,6})' + // first level domain- .com or .museum
    '(:[0-9]{1,4})?' + // 端口- :80
    '((/?)|' + // a slash isn't required if there is no file name
    "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
  var re = new RegExp(strRegex);
  //re.test()
  if (re.test(str_url)) {
    return true;
  } else {
    return false;
  }
}

export function checkStatusBtnShow (btn: any, common:any) {
  const sBtns = common?.showButtons || [];
  const showBtns = [...sBtns];

  for (let i = 0; i < showBtns.length; i++) {
    if (showBtns[i].key == btn) {
      return showBtns[i]?.show;
    }
  }
};
